import os.path
import sys
from PyQt5.QtWidgets import QMessageBox,QTableWidgetItem
from PyQt5.QtGui import QFont,QImage
from PyQt5.QtCore import Qt
import shutil
from mylibs.canvas import Canvas
from Module.xRayDetection.xray.ftp import FTP
from universal.translate.CHINESE import *
import threading
import cv2
import numpy as np

def Login2(host,name,code,logs_):   #登录函数
    try:
        if host == '' or name == '' or code == '':
            hepmsgbox = QMessageBox()
            hepmsgbox.setFont(QFont('Arial', 15))
            hepmsgbox.setWindowTitle("login ftp failed")
            cancleButton = hepmsgbox.addButton("cancle", QMessageBox.ActionRole)
            hepmsgbox.setText('\n' + "login ftp failed" + '\n')
            hepmsgbox.exec_()
            button = hepmsgbox.clickedButton()
            if button == cancleButton:
                return
        host = host.replace('ftp://', '')
        host = host.replace('/', '')
        Ftp = FTP(
                host=host,
                user=name,
                passwd=code,
                logger=logs_
            )
        return Ftp,host
    except Exception as err:
        print(err)

def TextBrowserNews(BoxMessages,news=None):
    BoxMessages.append(news)
    BoxMessages.moveCursor(BoxMessages.textCursor().StartOfLine)
    BoxMessages.moveCursor(BoxMessages.textCursor().Down)

def read(filename, default=None):
    try:
        with open(filename, 'rb') as f:
            return f.read()
    except:
        return default

    #     host = host.replace('ftp://', '')
    #     host = host.replace('/', '')
    #     my_ftp = MyFTP(host)
    #     my_ftp.connect(name, code)
    #     if my_ftp.isGetStatus():
    #         statusButton.setText("连接成功")
    #         self.statusButton.setStyleSheet("background-color:rgb(0,125,0);border:transparent")
    #         logger.info("连接成功")
    #     else:
    #         self.my_ftp.setStatus(False)
    #         hepmsgbox = QMessageBox(self)
    #         hepmsgbox.setFont(QFont('Arial', 15))
    #         hepmsgbox.setWindowTitle("login ftp failed")
    #         cancleButton = hepmsgbox.addButton("cancle", QMessageBox.ActionRole)
    #         hepmsgbox.setText('\n' + "login ftp failed" + '\n')
    #         hepmsgbox.exec_()
    #         button = hepmsgbox.clickedButton()
    #         if button == cancleButton:
    #             return
    # except Exception as err:
    #     logger.info(str(err))
    #     self.my_ftp.setStatus(False)
    #     hepmsgbox = QMessageBox(self)
    #     hepmsgbox.setFont(QFont('Arial', 15))
    #     hepmsgbox.setWindowTitle("login ftp failed")
    #     cancleButton = hepmsgbox.addButton("cancle", QMessageBox.ActionRole)
    #     hepmsgbox.setText('\n' + "login ftp failed" + '\n')
    #     hepmsgbox.exec_()
    #     button = hepmsgbox.clickedButton()
    #     if button == cancleButton:
    #         return
